//
// file : user_tmr.h
// date : 09/09/07
//
// there are two ways to drive a user-layer timer: 
//  (1) a constant ticking
//  (2) delta firing
// for (1) the master timer is awaked every tick period to check for
// expired user timers and call their timer subroutines
// for (2) the master timer is reset every time a user timer expires to the
// interval the next timer would expire. 
// timer context blocks are orgnized incrementally as a list. 
// timer context blocks are allocated by timer manager and recycled without
// user intervine.
// 
#ifndef USER_TMR_H_
#define USER_TMR_H_

#include "data_lnk.h"

typedef struct utimer_t
{
#define UT_FREE_TB     (1)
#define UT_MIN_DELAY   (2)

    unsigned long ut_flags;
    long          ut_delay;    // initial delay in milliseconds
    long          ut_cycle;    // repeat interval in milliseconds
    unsigned long ut_ticks;    // system ticks count to fire
    void*         ut_param;    // void* parameter
    void        (*ut_awake)(struct utimer_t *tb);

}  utimer_t; 


//
// timer list node
//
typedef struct utnode_t
{
    utnode_t     *next_;       // to next
    long          cycle;       // copy from timer.ut_cycle
    unsigned long ticks;       // ticks in milliseconds to fire
    utimer_t      timer;       // what user sees

}  utnode_t;

//
// master timer context
//
typedef struct uthead_t
{
    utnode_t     *alist;      // in-use nodes list
    utnode_t     *blist;      // head of free nodes list
    unsigned long tick_;      // minimum tick in milliseconds
    long          count;      // number of blocks in array
    utnode_t      array[0];   // array of blocks

}  uthead_t;

///////////////////////////////////////////////////////////////////////////////

#if defined (WIN32)

    #define tmr_gettick GetTickCount

#elif defined (__unix__)

#endif

///////////////////////////////////////////////////////////////////////////////

#endif // USER_TMR_H_
